package handler

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"log"
	"month5/client/client"
	"month5/client/request"
	"month5/common/proto/user"
	"month5/pkg"
	"net/http"
)

func Login(c *gin.Context) {
	var data request.Login
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 500,
			"Msg":  "参数错误",
			"Data": nil,
		})
		return
	}

	login, err := client.Login(c, &user.LoginReq{
		Name:     data.Name,
		Password: data.Password,
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 500,
			"Msg":  "失败",
			"Data": nil,
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"Code": 200,
		"Msg":  "成功",
		"Data": login,
	})
}

func EatList(c *gin.Context) {
	var data request.EatList
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 500,
			"Msg":  "参数错误",
			"Data": nil,
		})
		return
	}

	list, err := client.EatList(c, &user.EatListReq{Types: int32(data.Types)})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 500,
			"Msg":  "失败",
			"Data": nil,
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"Code": 200,
		"Msg":  "成功",
		"Data": list,
	})
}

func Upload(c *gin.Context) {
	file, _ := c.FormFile("file")
	log.Println(file.Filename)

	dst := "../pkg/static/" + file.Filename
	// 上传文件至指定的完整文件路径
	c.SaveUploadedFile(file, dst)
	pkg.Upload(dst, file.Filename)
	c.JSON(http.StatusOK, gin.H{
		"Code": 200,
		"Msg":  "成功",
		"Data": pkg.GetUrl(file.Filename),
	})
}

func PayBack(c *gin.Context) {
	out_trade_no := c.Request.FormValue("out_trade_no")
	fmt.Println(out_trade_no)

	trade_status := c.Request.FormValue("trade_status")
	fmt.Println(trade_status)

	back, err := client.PayBack(c, &user.PayBackReq{
		Status:  trade_status,
		OrderSn: out_trade_no,
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 500,
			"Msg":  "失败",
			"Data": nil,
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"Code": 200,
		"Msg":  "成功",
		"Data": back,
	})
}

func OrderList(c *gin.Context) {
	list, err := client.OrderList(c, &user.OrderListReq{})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 500,
			"Msg":  "失败",
			"Data": nil,
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"Code": 200,
		"Msg":  "成功",
		"Data": list,
	})
}

func CreatedOrder(c *gin.Context) {
	userId := c.GetUint("userId")
	var data request.CreatedOrder
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 500,
			"Msg":  "参数错误",
			"Data": nil,
		})
		return
	}

	order, err := client.CreatedOrder(c, &user.CreatedOrderReq{
		UserId:    int32(userId),
		TakeoutId: int32(data.TakeoutId),
		BuyNum:    int32(data.BuyNum),
		PayTypes:  int32(data.PayTypes),
		Coupon:    int32(data.Coupon),
		Notes:     data.Notes,
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 500,
			"Msg":  "失败",
			"Data": nil,
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"Code": 200,
		"Msg":  "成功",
		"Data": order,
	})
}

func AddCoupon(c *gin.Context) {
	userId := c.GetUint("userId")
	var data request.AddCoupon
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 500,
			"Msg":  "参数错误",
			"Data": nil,
		})
		return
	}

	coupon, err := client.AddCoupon(c, &user.AddCouponReq{
		UserId:       int32(userId),
		CouponsPrice: float32(data.CouponsPrice),
		Name:         data.Name,
		Status:       int32(data.Status),
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 500,
			"Msg":  "失败",
			"Data": nil,
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"Code": 200,
		"Msg":  "成功",
		"Data": coupon,
	})
}

func LookCoupons(c *gin.Context) {
	coupons, err := client.LookCoupons(c, &user.LookCouponsReq{})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 500,
			"Msg":  "失败",
			"Data": nil,
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"Code": 200,
		"Msg":  "成功",
		"Data": coupons,
	})
}
